package com.bysc.userservice.controller;


import com.bysc.userservice.entity.AclUser;
import com.bysc.userservice.entity.vo.LoginVo;
import com.bysc.userservice.service.AclUserService;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kkk
 * @since 2024-06-06
 */
@RestController
@Api(tags = "管理员管理")
@RequestMapping("/aclservice/acl-user")
public class AclUserController {
    @Autowired
    AclUserService aclUserService;
    @ApiOperation(value = "管理员登录")
    @PostMapping("login")
    public Results login(@RequestBody AclUser aclUser) {
        try {
            String token = aclUserService.login(aclUser);
            return Results.ok().data("token", token).message(MessageConstant.LOGIN_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,e.getMessage());
        }
    }

    @ApiOperation(value = "根据id获取管理员信息")
    @GetMapping("getUserInfo/{id}")
    public Results getUserInfo(@PathVariable String id) {
        try {
            AclUser user = aclUserService.getById(id);
            if (user == null) {
                throw new BaiYunException(ResultCode.ERROR,MessageConstant.VALIDATEUSER_ERROR);
            }
            return Results.ok().message(MessageConstant.QUERY_USERINFO_SUCCESS);
        } catch (BaiYunException e) {
            throw e;
        }
    }
    @ApiOperation(value = "根据id删除管理员信息")
    @DeleteMapping("deleteUserById/{id}")
    public Results deleteUserById(@PathVariable String id) {
        try {
            aclUserService.removeById(id);
            return Results.ok().message(MessageConstant.DELETE_USERINFO_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,e.getMessage());
        }
    }

    @ApiOperation(value = "根据id修改管理员信息")
    @PutMapping("updateUserById")
    public Results updateUserById(@RequestBody AclUser aclUser) {
        try {
            aclUserService.updateById(aclUser);
            return Results.ok().message(MessageConstant.EDIT_USERINFO_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR,e.getMessage());
        }
    }
}

